package com.tusuo.stds.ui.scenesetting;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.tusuo.stds.MyApp;
import com.tusuo.stds.R;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.SceneDetail;
import com.tusuo.stds.rest.SceneList;
import com.tusuo.stds.ui.main.SceneListAdapter;
import com.tusuo.stds.ui.scene.SceneActivity_;
import com.tusuo.stds.ui.scenedetail.SceneDetailActivity_;
import com.tusuo.stds.util.Utils;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;
import org.parceler.Parcels;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import rx.Subscriber;

/**
 * Created by developer on 17-8-28.
 */
@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(LockFragment.class)
@EFragment(R.layout.frag_scene_lock)
public class LockFragment extends Fragment{

    @Inject
    ApiHelper mApiHelper;

    @ViewById(R.id.frag_scene_lock_tv_new)
    TextView mTextViewNew;
    @ViewById(R.id.frag_scene_lock_list)
    ListView mListView;

    private List<SceneList.Scene> mDataList;

    @AfterInject
    void initDI() {
        Injector.initLockFragmentComponent().inject(this);
    }


    @AfterViews
    void initData() {
        mApiHelper.getSceneList(new GetSceneListSubscriber(),2,"");
    }

    void initAdapter(SceneList sceneList) {
        mDataList = sceneList.Data;
        mListView.setAdapter(new SceneListAdapter(getContext(),mDataList));
        mListView.setOverScrollMode(View.OVER_SCROLL_NEVER);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), SceneDetailActivity_.class);
                intent.putExtra(SceneList.Scene.EXTRA_NAME, Parcels.wrap(mDataList.get(position)));
                startActivity(intent);
            }
        });
    }


    @Click(R.id.frag_scene_lock_tv_new)
    void toNewOnlineActivity() {
        Intent intent = new Intent(getActivity(),NewOnlineActivity_.class);
        startActivity(intent);
    }

    private class GetSceneListSubscriber extends Subscriber<SceneList> {

        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onNext(SceneList sceneList) {
            initAdapter(sceneList);
        }
    }

}
